using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 药品管理系统
{
    public partial class frm_Order : Form
    {
        private DataTable OrderTable;
        private DataTable SelectedOrderTable;
        public frm_Order()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.dgv_Order.ReadOnly = true;
            this.dgv_Order.AllowUserToAddRows = false;
            this.dgv_Order.RowHeadersVisible = false;
            this.dgv_Order.BackgroundColor = Color.White;
            this.dgv_Order.AutoSizeColumnsMode =
                DataGridViewAutoSizeColumnsMode.AllCells;
            this.dgv_SelectedOrder.AllowUserToAddRows = false;
            this.dgv_SelectedOrder.RowHeadersVisible = false;
            this.dgv_SelectedOrder.BackgroundColor = Color.LightBlue;
            this.dgv_SelectedOrder.AutoSizeColumnsMode =
                DataGridViewAutoSizeColumnsMode.AllCells;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=KK的秘密;Database=MMS;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText =
                "SELECT No,Mname,Price,Amount ,Total FROM tb_order WHERE No NOT IN"
                + "(SELECT No FROM tb_orderselection WHERE OSno=@OSno);";
            sqlCommand.Parameters.AddWithValue("@OSno", "1001");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            this.OrderTable = new DataTable();
            this.SelectedOrderTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(this.OrderTable);
            sqlCommand.CommandText =
                "SELECT O.No,O.Mname,O.Price,O.Amount,O.Total,OS.Mname"
                + " FROM tb_order AS O JOIN tb_orderselection AS OS ON O.No=OS.Ono"
                + " WHERE OSno=@OSno;";
            sqlDataAdapter.Fill(this.SelectedOrderTable);
            sqlConnection.Close();
            this.dgv_Order.Columns.Clear();
            this.dgv_Order.DataSource = this.OrderTable;
            this.dgv_Order.Columns["No"].HeaderText = "订单编号";
            this.dgv_Order.Columns["Mname"].HeaderText = "药品名称";
            this.dgv_Order.Columns["Price"].HeaderText = "价格";
            this.dgv_Order.Columns["Amount"].HeaderText = "数量";
            this.dgv_Order.Columns["Total"].HeaderText = "总价";
            this.dgv_Order.Columns[this.dgv_Order.Columns.Count - 1].AutoSizeMode =
                DataGridViewAutoSizeColumnMode.Fill;
            this.dgv_SelectedOrder.Columns.Clear();
            this.dgv_SelectedOrder.DataSource = this.SelectedOrderTable;
            this.dgv_SelectedOrder.Columns["No"].HeaderText = "订购编号";
            this.dgv_SelectedOrder.Columns["Mname"].HeaderText = "药品名称";
            this.dgv_SelectedOrder.Columns["Price"].HeaderText = "价格";
            this.dgv_SelectedOrder.Columns["Amount"].HeaderText = "数量";
            this.dgv_SelectedOrder.Columns["Total"].HeaderText = "总价";
            this.dgv_SelectedOrder.Columns[this.dgv_SelectedOrder.Columns.Count - 1].AutoSizeMode =
                DataGridViewAutoSizeColumnMode.Fill;
            this.label1.Text =
                "共" + this.SelectedOrderTable.Compute("SUM(Total)", "").ToString() + "元";
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            
            if (this.dgv_Order.RowCount > 0)
            {
                DataRow
                    currentOrderRow = ((DataRowView)this.dgv_Order.CurrentRow.DataBoundItem).Row
                    , selectedOrderRow = this.SelectedOrderTable.NewRow();
                selectedOrderRow["No"] = currentOrderRow["No"];
                selectedOrderRow["Mname"] = currentOrderRow["Mname"];
                selectedOrderRow["Price"] =currentOrderRow["Price"];
                selectedOrderRow["Amount"] = currentOrderRow["Amount"];
                selectedOrderRow["Total"] = currentOrderRow["Total"];
                this.SelectedOrderTable.Rows.Add(selectedOrderRow);
                currentOrderRow.Delete();
                this.label1.Text =
                    "共" + this.SelectedOrderTable.Compute("SUM(Total)", "").ToString() + "元";
            }
        }

        private void btn_Remove_Click(object sender, EventArgs e)
        {
            if (this.dgv_SelectedOrder.RowCount > 0)
            {
                DataRow selectedOrderRow =
                    ((DataRowView)this.dgv_SelectedOrder.CurrentRow.DataBoundItem).Row;
                if (selectedOrderRow.RowState == DataRowState.Added)
                {
                    string orderNo = selectedOrderRow["No"].ToString();
                    DataRow deletedOrderRow =
                        this.OrderTable.Select("No='" + orderNo + "'", "", DataViewRowState.Deleted)[0];
                    deletedOrderRow.RejectChanges();
                    this.SelectedOrderTable.Rows.Remove(selectedOrderRow);
                    this.label1.Text =
                        "共" + this.SelectedOrderTable.Compute("SUM(Total)", "").ToString() + "元";
                }
            }
        }

        private void btn_Submit_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                              
            sqlConnection.ConnectionString =
                "Server=KK的秘密;Database=MMS;Integrated Security=sspi";                             
            SqlCommand insertCommand = new SqlCommand();                                                    
            insertCommand.Connection = sqlConnection;                                                      
            insertCommand.CommandText =
                "INSERT tb_orderselection(Ono,OSno,Mname,Total)"
                + "VALUES(@Ono,@OSno,@Mname,@Total);";                                              
            insertCommand.Parameters.AddWithValue("@OSno", "1001");
            insertCommand.Parameters.AddWithValue("@Ono", "4001");
            insertCommand.Parameters.Add("@Mname", SqlDbType.VarChar, 20, "Mname");
            insertCommand.Parameters.Add("@Total", SqlDbType.Int,0, "Total");
            SqlCommand updateCommand = new SqlCommand();                                                    
            updateCommand.Connection = sqlConnection;                                                       
            updateCommand.CommandText =                                                                    
                "UPDATE tb_orderselection"
                    + " SET Mname=@Mname,Total=@Total"
                    + " WHERE Ono=@Ono AND OSno=@OSno;";
            updateCommand.Parameters.AddWithValue("@OSno", "1001");
            updateCommand.Parameters.AddWithValue("@Ono", "4001");
            updateCommand.Parameters.Add("@Total", SqlDbType.Int, 0, "Total");
            updateCommand.Parameters.Add("@Mname", SqlDbType.VarChar, 20, "Mname");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           
            sqlDataAdapter.InsertCommand = insertCommand;                                                  
            sqlDataAdapter.UpdateCommand = updateCommand;                                                  
            sqlConnection.Open();                                                                          
            int rowAffected = sqlDataAdapter.Update(this.SelectedOrderTable);                              
            sqlConnection.Close();                                                                         
            MessageBox.Show("插入" + rowAffected.ToString() + "行。");    
        }

        private void 菜单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_Menu a = new frm_Menu();
            a.Show();
            this.Hide();
        }

        private void 登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_Login b = new frm_Login();
            b.Show();
            this.Hide();
        }

        private void 注册ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_Register c = new frm_Register();
            c.Show();
            this.Hide();
        }
    }
}

        
